package com.cheer.ba.view.activity;

import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cheer.ba.R;
import com.cheer.ba.constant.ConstantTag;
import com.cheer.ba.constant.IntentKey;
import com.cheer.ba.constant.Type;
import com.cheer.ba.event.BaseEvent;
import com.cheer.ba.model.CouponCardModel;
import com.cheer.ba.presenter.impl.CardGroupPresenter;
import com.cheer.ba.utils.Currency;
import com.cheer.ba.utils.StringUtils;
import com.cheer.ba.utils.ToastUtil;
import com.cheer.ba.view.base.BaseCommAty;
import com.cheer.ba.view.base.CommonAdapter;
import com.cheer.ba.view.base.IViewController;
import com.cheer.ba.view.base.ViewHolder;
import com.cheer.ba.widget.XListView.XListView;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 优惠券
 */
public class DiscountCouponAty extends BaseCommAty implements XListView.IXListViewListener, AdapterView.OnItemClickListener {

    //没有数据显示界面
    @BindView(R.id.id_no_data_view)
    View mNoDataView;
    @BindView(R.id.id_pic_img)
    ImageView mImgNoPic;
    @BindView(R.id.id_search_info_txt)
    TextView mTxtNoData;


    @BindView(R.id.id_card_group_listview)
    XListView mListView;
    private CommonAdapter<CouponCardModel> mCardGroupAdp;
    //卡券列表
    public List<CouponCardModel> mCardGroups = new ArrayList<CouponCardModel>();

    //支付金额
    private String money;

    private String mPayMoney;

    @Override
    public View loadView(LayoutInflater inflater, View childView) {
        return super.loadView(inflater, inflater.inflate(R.layout.aty_discount_coupon, null, false));
    }

    @Override
    public boolean hasDataInPage() {
        return !mCardGroups.isEmpty();
    }

    @Override
    public void initPresenter() {
        presenter = new CardGroupPresenter();
    }

    @Override
    public void initView() {
        money = getIntent().getStringExtra(IntentKey.General.KEY_PAY_MONEY);
        if (StringUtils.isEmpty(money)) {
            mPayMoney = "0";
        } else {
            mPayMoney = (int) (Double.parseDouble(money) * 100) + "";
        }
        mNoDataView.setVisibility(View.GONE);
        mListView.setVisibility(View.VISIBLE);
        mListView.setPullRefreshEnable(true);
        mListView.setPullLoadEnable(true);
        mListView.setXListViewListener(this);
        mCardGroupAdp = new CommonAdapter<CouponCardModel>(aty, mCardGroups, R.layout.adp_card_group) {
            @Override
            public void convert(ViewHolder helper, final CouponCardModel item) {
                if (null != item) {
                    RelativeLayout mLeftView = (RelativeLayout) helper.getViewById(R.id.id_bg_left);
                    RelativeLayout mRightView = (RelativeLayout) helper.getViewById(R.id.id_right_view);
                    TextView mTxtMoneySign = (TextView) helper.getViewById(R.id.id_money_sign_txt);
                    TextView mTxtCardPrice = (TextView) helper.getViewById(R.id.id_card_price_txt);
                    TextView mTxtCardDesc = (TextView) helper.getViewById(R.id.id_card_price_desc_txt);
                    TextView mTxtCardName = (TextView) helper.getViewById(R.id.id_card_name_txt);
                    TextView mTxtTip1 = (TextView) helper.getViewById(R.id.id_card_desc_txt);
                    TextView mTxtTip2 = (TextView) helper.getViewById(R.id.id_card_time_txt);
                    Button mBtnUse = (Button) helper.getViewById(R.id.id_to_use_card_btn);

                    mLeftView.setBackgroundResource(R.drawable.ic_ticket_off);
                    mTxtMoneySign.setTextColor(getResources().getColor(R.color.color_999999));
                    mTxtCardPrice.setTextColor(getResources().getColor(R.color.color_999999));

//                    mTxtCardPrice.setText(Currency.returnDollar(item.getCouponValue() + "").replace("元", ""));
                    mTxtCardDesc.setTextColor(getResources().getColor(R.color.color_999999));
                    int money = item.getRuleMoneyMin();
                    if (money <= 0) {
                        mTxtCardDesc.setText("通用券");
                    } else {
                        mTxtCardDesc.setText("满" + Currency.returnDollar(money + "") + "可用");
                    }
                    mTxtCardName.setTextColor(getResources().getColor(R.color.color_999999));
                    mTxtTip1.setTextColor(getResources().getColor(R.color.color_999999));
                    if (item.getCouponType() == Type.voucherType_1) {
                        mTxtTip1.setText("折扣券");
                        mTxtCardName.setText("折扣券");
                        String m = StringUtils.removePoint(Double.parseDouble(item.getCouponValue()) * 100 + "") + "折";
                        mTxtCardPrice.setText(m);
                    } else if (item.getCouponType() == Type.voucherType_2) {
                        mTxtTip1.setText("代金券");
                        mTxtCardName.setText("代金券");
                        mTxtCardPrice.setText(Currency.returnDollar(item.getCouponValue()));
                    } else if (item.getCouponType() == Type.voucherType_3) {
                        mTxtTip1.setText("特价券");
                        mTxtCardName.setText("特价券");
                    } else if (item.getCouponType() == Type.voucherType_4) {
                        mTxtTip1.setText("减免券");
                        mTxtCardName.setText("减免券");
                    } else if (item.getCouponType() == Type.voucherType_5) {
                        mTxtTip1.setText("提货券");
                        mTxtCardName.setText("提货券");
                    } else if (item.getCouponType() == Type.voucherType_6) {
                        mTxtTip1.setText("兑换券");
                        mTxtCardName.setText("兑换券");
                    } else if (item.getCouponType() == Type.voucherType_7) {
                        mTxtTip1.setText("满就送");
                        mTxtCardName.setText("满就送");
                    }
                    mBtnUse.setVisibility(View.GONE);
                    if (item.getCouponDetailsStatus() == 0) {
                        //未使用
                        mLeftView.setBackgroundResource(R.drawable.ic_ticket_on);
                        mTxtMoneySign.setTextColor(getResources().getColor(R.color.color_e69216));
                        mTxtCardPrice.setTextColor(getResources().getColor(R.color.color_e69216));

                        mTxtCardDesc.setTextColor(getResources().getColor(R.color.color_e69216));

                        mRightView.setBackgroundResource(R.drawable.ic_ticket_bg);
                        mTxtCardName.setTextColor(getResources().getColor(R.color.color_333333));
                        mTxtTip1.setTextColor(getResources().getColor(R.color.color_333333));
                        Drawable d = getResources().getDrawable(R.drawable.ic_card_point_on);
                        d.setBounds(0, 0, d.getMinimumWidth(), d.getMinimumHeight());
                        mTxtTip1.setCompoundDrawables(d, null, null, null);
                        mTxtTip2.setTextColor(getResources().getColor(R.color.color_999999));
                        mTxtTip2.setCompoundDrawables(d, null, null, null);
                        mTxtTip2.setTextColor(getResources().getColor(R.color.color_333333));
                        mBtnUse.setVisibility(View.VISIBLE);
                        mBtnUse.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                //去使用
                                //优惠券选择返回
                                BaseEvent event = new BaseEvent();
                                event.type = IntentKey.Coupon.COUPON_MONEY;
                                event.id = item.getUid();
                                event.money = item.getCouponValue();
                                event.couponType = item.getCouponType();
                                EventBus.getDefault().post(event);
                                finish();
                            }
                        });
                    } else if (item.getCouponDetailsStatus() == 1) {
                        //使用记录
                        mRightView.setBackgroundResource(R.drawable.ic_ticket_bg_on);
                        Drawable d = getResources().getDrawable(R.drawable.ic_card_point_off);
                        d.setBounds(0, 0, d.getMinimumWidth(), d.getMinimumHeight());
                        mTxtTip1.setCompoundDrawables(d, null, null, null);
                        mTxtTip2.setTextColor(getResources().getColor(R.color.color_999999));
                        mTxtTip2.setCompoundDrawables(d, null, null, null);
                    } else if (item.getCouponDetailsStatus() == 2) {
                        //已过期
                        mRightView.setBackgroundResource(R.drawable.ic_ticket_bg_off);
                        Drawable d = getResources().getDrawable(R.drawable.ic_card_point_off);
                        d.setBounds(0, 0, d.getMinimumWidth(), d.getMinimumHeight());
                        mTxtTip1.setCompoundDrawables(d, null, null, null);
                        mTxtTip2.setTextColor(getResources().getColor(R.color.color_999999));
                        mTxtTip2.setCompoundDrawables(d, null, null, null);
                    }

                    mTxtTip2.setText(StringUtils.getCurrentDay(item.getStartTime()) + "至" + StringUtils.getCurrentDay(item.getEndTime()));
                }
            }
        };
        mListView.setAdapter(mCardGroupAdp);
//        mListView.setOnItemClickListener(this);
    }

    @Override
    public void initData() {
        ((CardGroupPresenter) presenter).getMyDiscountList(false, mPayMoney);
    }

    @Override
    public <T> void updateViewWithLoadMore(T t, boolean loadMore, String tag) {
        if (null != t) {
            if (TextUtils.equals(tag, ConstantTag.MY_CARD_GROUP)) {
                //我的优惠券
                mListView.stopRefresh();
                mListView.stopLoadMore();
                List<CouponCardModel> myCards = (List<CouponCardModel>) t;
                if (!loadMore) {
                    mCardGroups.clear();
                }
                if (!myCards.isEmpty()) {
                    mCardGroups.addAll(myCards);
                    mCardGroupAdp.notifyDataSetChanged();
                } else {
                    hideFoot();
                    if (loadMore) {
                        ToastUtil.showToast(getString(R.string.no_more_data));
                    } else {
                        //暂无数据
                        mNoDataView.setVisibility(View.VISIBLE);
                        mListView.setVisibility(View.GONE);
                        mTxtNoData.setText("暂无优惠券信息");
                    }
                }
            }
        }
    }

    @Override
    public void onLoadingStatus(IViewController mView, boolean isHandler, int loadingStatus, int flag, String string, boolean loadMore) {
        mListView.stopRefresh();
        mListView.stopLoadMore();
        super.onLoadingStatus(mView, isHandler, loadingStatus, flag, string, loadMore);
    }

    @Override
    public void onRefresh() {
        if (mListView.getFooterViewsCount() <= 0) {
            mListView.addFooterView(mListView.mFooterView);
        }
        ((CardGroupPresenter) presenter).getMyDiscountList(false, mPayMoney);
    }

    @Override
    public void onLoadMore() {
        ((CardGroupPresenter) presenter).getMyDiscountList(true, mPayMoney);
    }

    private void hideFoot() {
        mListView.removeFooterView(mListView.getFooterView());
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        CouponCardModel model = (CouponCardModel) mCardGroupAdp.getItem(position);
        if (model.getCouponDetailsStatus() == 0) {

            //优惠券选择返回
            BaseEvent event = new BaseEvent();
            event.type = IntentKey.Coupon.COUPON_MONEY;
            event.id = model.getUid();
            event.money = model.getCouponValue();
            event.couponType = model.getCouponType();
            EventBus.getDefault().post(event);
            finish();
        }
    }
}
